#!/usr/bin/env python

import sys, getopt, switch

def usage():
	print "usage: get_ios_version [-h] [-a address]"
	print "\t-a or --address\t specify the IP address of the switch"
	print "\t   (if not specified the command will be executed on every switchs)"
	print "\t-h\t\t display this help"
	print

def run_get_ios_version(list_switchs):
	for ip in list_switchs:
		print "Connecting to " + ip + "..."
		tn = switch.connect(ip)
		if not tn:
			print "Error while connecting to " + ip
			continue
		name = switch.enable(tn)
		if not name:
			print "Error while connecting to " + ip
			continue
		print "Connected on " + name
		version = switch.get_ios_version(tn)
		print name + " is running:"
		print version
		switch.quit(tn)
		print

def main(argv):
	try:
		opts, args = getopt.getopt(argv[1:], "ha:", ["help", "address="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
	list_switchs = []
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit()
		elif opt in ("-a", "--address"):
			list_switchs.append(arg)
	if list_switchs == []:
		list_switchs = switch.get_switch_list()
	run_get_ios_version(list_switchs)

if __name__ == "__main__":
    main(sys.argv)
